package com.zlzlib.libindex.decoration;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by zlz on 2019/10/31.
 * RecyclerView   LinearLayoutManager 的 分割 线
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    //系统自带的属性，宽高各2dp
    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    //横向
    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    //竖向
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    //分割线
    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(Context context) {
        init(context, VERTICAL_LIST);
    }


    public DividerItemDecoration(Context context, int orientation) {
        init(context, orientation);
    }

    private void init(Context context, int orientation) {
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    //设置分割层的方向
    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    public DividerItemDecoration setDivider(Drawable mDivider) {
        this.mDivider = mDivider;
        return this;
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        ((LinearLayoutManager) parent.getLayoutManager()).setOrientation(mOrientation);
        if (mDivider != null) {
            //需要传入画布和当前 RecyclerView
            if (mOrientation == HORIZONTAL_LIST) {
                drawHorizontal(c, parent);
            } else {
                drawVertical(c, parent);
            }
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        //item之间的位移量
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }

    //绘制竖直方向的横线
    private void drawVertical(Canvas c, RecyclerView parent) {
        int left = parent.getPaddingLeft();//横线左右随父容器
        int right = parent.getWidth() - parent.getPaddingRight();
        for (int i = 0; i < parent.getChildCount(); i++) {
            //遍历 RecyclerView 的每个子view
            View child = parent.getChildAt(i);
            //确定分割的位置
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);//设置分割线的区域
            mDivider.draw(c);//绘制到画布
        }
    }


    //绘制水平方向的竖线
    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int top = parent.getPaddingTop();//竖线上下随父容器
        int bottom = parent.getHeight() - parent.getPaddingBottom();
        for (int i = 0; i < parent.getChildCount(); i++) {
            //遍历 RecyclerView 的每个子view
            View child = parent.getChildAt(i);
            //确定分割的位置
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left = child.getRight() + params.rightMargin;
            int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);//设置分割线的区域
            mDivider.draw(c);//绘制到画布
        }
    }
}
